package com.shop.dao.impl;

import java.util.List;

import org.apache.ibatis.session.SqlSession;
import org.springframework.stereotype.Repository;

import com.shop.dao.UserDao;
import com.shop.domain.Cart;
import com.shop.domain.Favorite;
import com.shop.domain.MessageCenter;
import com.shop.domain.Order;
import com.shop.domain.User;
import com.shop.util.MyBatisUtil;

/**
 * 
 * @author wenjuanhenxing
 * @version V1.0 UserDaoImpl.java 2016-2-10 下午1:15:27
 */
@Repository("userDaoImpl")
public class UserDaoImpl implements UserDao {

	@Override
	public boolean usernameExists(String username) {
		if (username == null) {
			return false;
		}
		return getUserByUsername(username) == null ? true : false;
	}

	@Override
	public boolean emailExists(String email) {
		if (email == null) {
			return false;
		}
		return getUserByEmail(email) == null ? true : false;
	}

	@Override
	public User getUserByUsername(String username) {
		if (username == null) {
			return null;
		}
		SqlSession sqlSession = MyBatisUtil.getSqlSession();
		User user = sqlSession.selectOne("com.shop.dao.mapping.UserMapper"
				+ ".getUserByUsername", username);
		MyBatisUtil.closeSession(sqlSession);
		return user;
	}

	@Override
	public User getUserByEmail(String email) {
		if (email == null) {
			return null;
		}
		SqlSession sqlSession = MyBatisUtil.getSqlSession();
		User user = sqlSession.selectOne("com.shop.dao.mapping.UserMapper"
				+ ".getUserByEmail", email);
		MyBatisUtil.closeSession(sqlSession);
		return user;
	}

	@Override
	public boolean addUser(User user) {
		if (user == null) {
			return false;
		}
		SqlSession sqlSession = MyBatisUtil.getSqlSession(true);
		int rowsAffected = sqlSession.insert("com.shop.dao.mapping.UserMapper"
				+ ".addUser", user);
		MyBatisUtil.closeSession(sqlSession);
		return rowsAffected == 1;
	}

	@Override
	public List<User> getAllUsers() {
		SqlSession sqlSession = MyBatisUtil.getSqlSession();
		List<User> users = sqlSession.selectList(User.class.getName()
				+ "Mapper" + ".getAllUsers");
		MyBatisUtil.closeSession(sqlSession);
		return users;
	}

	@Override
	public User getUserById(Long id) {
		SqlSession sqlSession = MyBatisUtil.getSqlSession();
		User user = sqlSession.selectOne("com.shop.dao.mapping.UserMapper"
				+ ".getUserById", id);
		MyBatisUtil.closeSession(sqlSession);
		return user;
	}

	@Override
	public boolean finishCertify(User user) {
		if (user == null) {
			return false;
		}
		SqlSession sqlSession = MyBatisUtil.getSqlSession(true);
		int rowsAffeted = sqlSession.update("com.shop.dao.mapping.UserMapper"
				+ ".updateFinishCertify", user);
		MyBatisUtil.closeSession(sqlSession);
		return rowsAffeted == 1;
	}

	@Override
	public boolean updateFreezeStatus(User user) {
		SqlSession sqlSession = MyBatisUtil.getSqlSession(true);
		int rowsAffeted = sqlSession.update("com.shop.dao.mapping.UserMapper"
				+ ".updateFreezeStatus", user);
		MyBatisUtil.closeSession(sqlSession);
		return rowsAffeted == 1;
	}

	@Override
	public List<Cart> getCarts(Long id) {
		SqlSession sqlSession = MyBatisUtil.getSqlSession();
		List<Cart> carts = sqlSession.selectList(User.class.getName()
				+ "Mapper" + ".getCarts", id);
		MyBatisUtil.closeSession(sqlSession);
		return carts;
	}

	@Override
	public List<Favorite> getFavorites(Long id) {
		SqlSession sqlSession = MyBatisUtil.getSqlSession();
		List<Favorite> favorites = sqlSession.selectList(User.class.getName()
				+ "Mapper" + ".getFavorites", id);
		MyBatisUtil.closeSession(sqlSession);
		return favorites;
	}

	@Override
	public List<MessageCenter> getReceiveMessages(Long id) {
		SqlSession sqlSession = MyBatisUtil.getSqlSession();
		List<MessageCenter> receiveMessages = sqlSession.selectList(
				"com.shop.dao.mapping.UserMapper" + ".getReceiveMessages", id);
		MyBatisUtil.closeSession(sqlSession);
		return receiveMessages;
	}

	@Override
	public List<MessageCenter> getSendMessages(Long id) {
		SqlSession sqlSession = MyBatisUtil.getSqlSession();
		List<MessageCenter> sendMessages = sqlSession.selectList(
				"com.shop.dao.mapping.UserMapper" + ".getSendMessages", id);
		MyBatisUtil.closeSession(sqlSession);
		return sendMessages;
	}

	@Override
	public List<Order> getBuyOrders(Long id) {
		SqlSession sqlSession = MyBatisUtil.getSqlSession();
		List<Order> buyOrders = sqlSession.selectList(User.class.getName()
				+ "Mapper" + ".getBuyOrders", id);
		MyBatisUtil.closeSession(sqlSession);
		return buyOrders;
	}

	@Override
	public List<Order> getSaleyOrders(Long id) {
		SqlSession sqlSession = MyBatisUtil.getSqlSession();
		List<Order> saleOrders = sqlSession.selectList(User.class.getName()
				+ "Mapper" + ".getSaleOrders", id);
		MyBatisUtil.closeSession(sqlSession);
		return saleOrders;
	}

	@Override
	public boolean updatePassword(User user) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean updateEmail(User user) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean updatePhone(User user) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean updateAddress(User user) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean updateCredit(User user) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean updateRealName(User user) {
		SqlSession sqlSession = MyBatisUtil.getSqlSession(true);
		int rowsAffected = sqlSession.update("com.shop.dao.mapping.UserMapper"
				+ ".updateRealName", user);
		MyBatisUtil.closeSession(sqlSession);
		return rowsAffected == 1;
	}

	@Override
	public boolean updateCertifyStatus(User user) {
		SqlSession sqlSession = MyBatisUtil.getSqlSession(true);
		// Map<String, Object> map = new HashMap<String, Object>();
		// map.put("id", user.getId());
		// map.put("certifyStatus", (user.getCertifyStatus() == true) ? "b'1'"
		// : "b'0'");
		String stmt = "com.shop.dao.mapping.UserMapper";
		stmt += (user.getCertifyStatus() == true) ? ".updateCertifyStatusTrue"
				: ".updateCertifyStatusFalse";
		int rowsAffected = sqlSession.update(stmt, user.getId());
		MyBatisUtil.closeSession(sqlSession);
		return rowsAffected == 1;
	}

}
